package com.freewater.springboot.demo.spel.compliler;

import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.SpelCompilerMode;
import org.springframework.expression.spel.SpelParserConfiguration;
import org.springframework.expression.spel.standard.SpelExpressionParser;

public class Main {
    public static void main(String[] args) {
        // 开启编译器（编译运行，性能更高），模式为SpelCompilerMode.IMMEDIATE
        // 注意开启编译器有限制，spring默认没有开启编译器，spEl是以解释模式运行的（性能低）
        // 限制详见:https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#expressions-evaluation
        SpelParserConfiguration configuration = new SpelParserConfiguration(SpelCompilerMode.IMMEDIATE, Main.class.getClassLoader());
        ExpressionParser parser = new SpelExpressionParser(configuration);
        final Expression expression = parser.parseExpression("payload");
        //
        MyMessage myMessage = new MyMessage("Hello World");
        final Object payload = expression.getValue(myMessage);
        System.out.println(payload);
    }

    static class MyMessage {

        final String payload;

        MyMessage(String payload) {
            this.payload = payload;
        }

        public String getPayload() {
            return payload;
        }
    }
}
